package com.mobisys.live.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mobisys.live.dao.model.Live;
import com.mobisys.live.dao.model.Video;
import com.mobisys.live.response.Response;
import com.mobisys.live.service.LiveService;
import com.mobisys.live.service.VideoService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

import static com.mobisys.live.config.ResponseCode.*;

@RestController
public class HomeController {
    @Autowired
    private VideoService videoService;
    @Autowired
    private LiveService liveService;
    private Logger logger = LogManager.getLogger(HomeController.class.getName());
    @RequestMapping("/home")
    public Response homepage() {
        JSONObject JsonObject = new  JSONObject() ;
        Response response = new Response();



        List<Video> videopartylist = new ArrayList<>();
        List<Video> videoscholarlist = new ArrayList<>();
        List<Live> Livescholarlist = new ArrayList<>();
        List<Live> Livepartylist = new ArrayList<>();
        //学术类型定为0 每次返回固定的前10个
        videoscholarlist = videoService.findAll(1,2,"0");
        //晚会类型定为1
        videopartylist = videoService.findAll(1,2,"1");
        Livescholarlist = liveService.findAll(1,2,"0");
        Livepartylist = liveService.findAll(1,2,"1");
        if(videoscholarlist.isEmpty()){
            logger.debug("学术视频查找失败");
            response.setRetureValue(VIDEO_TYPE_FINDALL_ERROR);
            response.setRetureMsg(MSG_VIDEO_TYPE_FINDALL_ERROR);
        }else if(videopartylist.isEmpty()){
            logger.debug("晚会视频查找失败");
            response.setRetureValue(VIDEO_TYPE_FINDALL_ERROR);
            response.setRetureMsg(MSG_VIDEO_TYPE_FINDALL_ERROR);
        }else if(Livescholarlist.isEmpty()) {
            logger.debug("学术直播查找失败");
            response.setRetureValue(LIVE_TYPE_FINDALL_ERROR);
            response.setRetureMsg(MSG_LIVE_TYPE_FINDALL_ERROR);
        }else if(Livepartylist.isEmpty()) {
            logger.debug("晚会直播查找失败");
            response.setRetureValue(LIVE_TYPE_FINDALL_ERROR);
            response.setRetureMsg(MSG_LIVE_TYPE_FINDALL_ERROR);
        }
        if(!(videoscholarlist.isEmpty())&&!(videopartylist.isEmpty())
                &&!(Livescholarlist.isEmpty())&&!(Livepartylist.isEmpty())) {
            response.setRetureValue(SUCCESS);
            response.setRetureMsg(MSG_SUCCESS);
        }
        JsonObject.put("videoscholarlist",videoscholarlist);
        JsonObject.put("videopartylist",videopartylist);
        JsonObject.put("Livescholarlist",Livescholarlist);
        JsonObject.put("Livepartylist",Livepartylist);
        response.setRetureData(JsonObject);
        return response;
    }
}
